package org.iabako.server.dao.user;

import org.iabako.server.dao.JpaDAO;
import org.iabako.shared.entity.user.Enterprise;
import org.iabako.shared.entity.user.UserPayPalToken;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by jose on 16/12/14.
 */

@Repository("userPayPalTokenDAO")
public class UserPayPalTokenDAO extends JpaDAO<Long, UserPayPalToken> {

    @Override
    protected String getUniqueNumberVerificationQuery() {
        return null;
    }

    @Override
    protected String getLastNumberQuery() {
        return null;
    }

    public UserPayPalToken getUserPayPalTokenByTxId(String iabakoTxId) {
        String query = "SELECT userPayPalToken FROM UserPayPalToken userPayPalToken WHERE userPayPalToken.transactionId = :transactionId";
        List<UserPayPalToken> tokens = entityManager.createQuery(query).setParameter("transactionId", iabakoTxId).getResultList();
        return tokens == null || tokens.isEmpty() ? null : tokens.get(0);
    }

    public List<UserPayPalToken> getNotPayedUserPayPalTokenByEmail(Enterprise enterprise) {
        String query = "SELECT userPayPalToken FROM UserPayPalToken userPayPalToken join userPayPalToken.payer payer " +
                "WHERE userPayPalToken.transactionId IS NOT NULL AND payer.enterprise= :enterprise AND userPayPalToken.payed = 0 AND userPayPalToken.canceled = 0";

        return entityManager.createQuery(query).setParameter("enterprise", enterprise).getResultList();
    }
}
